package com.buaa.aimall.goodsModule.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.aimall.Utils.Enum.PaymentStatus;
import com.buaa.aimall.goodsModule.Entity.GoodsOrderEntity;
import com.buaa.aimall.goodsModule.Enum.GoodsOrderSortTypeEnum;
import com.buaa.aimall.goodsModule.Enum.TransactionSortTypeEnum;
import com.buaa.aimall.goodsModule.Mapper.TransactionOrderMapper;
import com.buaa.aimall.goodsModule.Entity.TransactionOrderEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buaa.aimall.Utils.PageReturnResult;
import com.buaa.aimall.goodsModule.DTO.FilterTransactionOrderDTO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;

@Service
public class TransactionOrderServiceImpl extends ServiceImpl<TransactionOrderMapper, TransactionOrderEntity> implements TransactionOrderService {

    @Override
    public long getUnpaidCount(String userId) {
        LambdaQueryWrapper<TransactionOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TransactionOrderEntity::getBuyerID, userId);
        queryWrapper.eq(TransactionOrderEntity::getPaymentStatus, PaymentStatus.PAYING);
        return this.count(queryWrapper);
    }

    @Override
    public int getPaymentAmountTodayOrThisMonth(String userId, boolean isToday) {
        LambdaQueryWrapper<TransactionOrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TransactionOrderEntity::getBuyerID, userId);
        queryWrapper.eq(TransactionOrderEntity::getPaymentStatus, PaymentStatus.SUCCESS);
        if (isToday) {
            queryWrapper.ge(TransactionOrderEntity::getCreateTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        } else {
            YearMonth currentMonth = YearMonth.now();
            queryWrapper.ge(TransactionOrderEntity::getCreateTime, LocalDateTime.of(currentMonth.atDay(1), LocalTime.MIN));
        }

        this.list(queryWrapper);
        int amount = 0;
        for (TransactionOrderEntity entity : this.list(queryWrapper)) {
            amount += entity.getTotalAmount();
        }
        return amount;
    }

    @Override
    public PageReturnResult<TransactionOrderEntity> filterTransactionOrder(FilterTransactionOrderDTO filterDTO) {
        PageReturnResult<TransactionOrderEntity> result = new PageReturnResult<>();
        LambdaQueryWrapper<TransactionOrderEntity> queryWrapper = new LambdaQueryWrapper<>();

        // 添加筛选条件
        if (filterDTO.getId() != null) {
            queryWrapper.eq(TransactionOrderEntity::getId, filterDTO.getId());
        }
        if (filterDTO.getBuyerID() != null) {
            queryWrapper.eq(TransactionOrderEntity::getBuyerID, filterDTO.getBuyerID());
        }
        if (filterDTO.getPaymentStatus() != null) {
            queryWrapper.eq(TransactionOrderEntity::getPaymentStatus, filterDTO.getPaymentStatus());
        }
        if (filterDTO.getAmountMin() != null) {
            queryWrapper.ge(TransactionOrderEntity::getTotalAmount, filterDTO.getAmountMin());
        }
        if (filterDTO.getAmountMax() != null) {
            queryWrapper.le(TransactionOrderEntity::getTotalAmount, filterDTO.getAmountMax());
        }
        if (filterDTO.getCreateTimeStart() != null) {
            queryWrapper.ge(TransactionOrderEntity::getCreateTime, filterDTO.getCreateTimeStart());
        }
        if (filterDTO.getCreateTimeEnd() != null) {
            queryWrapper.le(TransactionOrderEntity::getCreateTime, filterDTO.getCreateTimeEnd());
        }
        if (filterDTO.getSort() != null) {
            addSortConditions(queryWrapper, filterDTO.getSort().getCode());
        } else {
            addSortConditions(queryWrapper, TransactionSortTypeEnum.DEFAULT.getCode());
        }

        // 获取总记录数
        long totalCount = this.count(queryWrapper);
        result.setTotalCount(totalCount);

        // 分页处理
        if (filterDTO.getPageSize() != null && filterDTO.getCurrentPage() != null) {
            Page<TransactionOrderEntity> page = new Page<>(filterDTO.getCurrentPage(), filterDTO.getPageSize());
            Page<TransactionOrderEntity> pageResult = this.page(page, queryWrapper);
            result.setRecords(pageResult.getRecords());
        } else {
            result.setRecords(this.list(queryWrapper));
        }

        return result;
    }

    private void addSortConditions(LambdaQueryWrapper<TransactionOrderEntity> queryWrapper, String sortType) {
        TransactionSortTypeEnum.fromCode(sortType)
                .getSortFunction()
                .accept(queryWrapper);
    }
}
